package com.hecun.crm.service.impl;

import com.hecun.crm.mapper.CustomerMapper;
import com.hecun.crm.pojo.Customer;
import com.hecun.crm.pojo.QueryVo;
import com.hecun.crm.service.CustomerService;
import com.hecun.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Page<Customer> queryCustomerByQueryVo(QueryVo queryVo) {
        //组装好了queryVo对象
        queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());

        //查询总条数和数据集合,准备构造Page对象
        int total = customerMapper.queryCountByQueryVo(queryVo);
        List<Customer> list = customerMapper.queryCustomerByQueryVo(queryVo);

        //构造Page对象
        Page<Customer> page = new Page<>();
        page.setTotal(total);
        page.setRows(list);
        page.setSize(queryVo.getRows());
        page.setPage(queryVo.getPage());

        return page;
    }

    @Override
    public Customer queryCustomerById(Long id) {
        Customer customer = customerMapper.queryCustomerById(id);
        return customer;
    }

    @Override
    public void updateCustomerById(Customer customer) {
        customerMapper.updateCustomerById(customer);
    }

    @Override
    public void deleteCustomerById(Long id) {
        customerMapper.deleteCustomerById(id);
    }
}
